package com.jungle.mvvm.sample.aspect;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.jungle.mvvm.sample.app.MyApplication;
import com.jungle.mvvm.sample.ui.login.LoginActivity;
import com.jungle.mvvm.utils.KLog;
import com.jungle.mvvm.utils.SPUtils;
import com.jungle.mvvm.utils.StringUtils;
import com.jungle.mvvm.utils.ToastUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;


/**
 * @ClassName AuthorizeAspect
 * @Description 用户权限Aspect
 * @Author Daniel
 * @Version V1.0.0
 */
@Aspect
public class AuthorizeAspect {
    private final static String TAG = "AuthorizeAspect";

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Around("execution(@com.jungle.mvvm.sample.annotation.Authorize * *(..))")
    public void doAuthorize(final ProceedingJoinPoint joinPoint) {
        try {
            KLog.e(TAG, "本次的日志记录情况为：时间是：" + System.currentTimeMillis());
            String userId = SPUtils.getInstance().getString("userId");
            if (!StringUtils.isTrimEmpty(userId)) {
                joinPoint.proceed();
            } else {
                Intent intent = new Intent(MyApplication.getInstance(), LoginActivity.class);
                MyApplication.getInstance().startActivity(intent);
                ToastUtils.showShort("你还没有登录，请先登录！");
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
